package com.gzu.shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gzu.shop.base.BaseController;
import com.gzu.shop.common.Result;
import com.gzu.shop.entity.OpLog;
import com.gzu.shop.entity.User;
import com.gzu.shop.mapper.UserMapper;
import com.gzu.shop.param.SearchCondition;
import com.gzu.shop.service.OpLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Clown
 */
@RestController
@RequestMapping("/op-log")
@AllArgsConstructor
@Slf4j
@Tag(name = "用户操作相关接口")
public class OpLogController extends BaseController<OpLogService, OpLog> {
    private UserMapper userMapper;
    @Override
    @Operation(summary = "重新封装数据")
    @PostMapping("/page/{startPage}/{pageSize}")
    public Result<IPage<OpLog>> page(@PathVariable int startPage,
                                     @PathVariable int pageSize,
                                     @RequestBody(required = false) SearchCondition<OpLog> searchCondition) {

//        log.info("searchCondition:{}", searchCondition);
        Result<IPage<OpLog>> page = super.page(startPage, pageSize, searchCondition);
        List<OpLog> records = page.getData().getRecords();

        for (OpLog opLog : records) {
            User user = userMapper.selectById(opLog.getUserId());
            opLog.setUsername(user.getUsername());
        }
        page.getData().setRecords(records);
        return page;
    }
}
